// components/question/index.js
import { IndexModel } from "../../models/index.js";
const indexModel = new IndexModel();
Component({

    /**
     * 组件的属性列表
     */
    properties: {
        titleMsg:"",
        topicCount:0,
        topicData:null,
        modeNum:0,//0答题模式；1背题模式
        topicIndex:0,//当前数组的第几道题
        userAnswer:'',//用户的答案
        mockId:0,
        examTime: 0,
        timeStatus:false,
    },

    /**
     * 组件的初始数据
     */
    data: {
        feedBackBtnColor:'#3e87f59d',//错题反馈按钮颜色
        correctDivShow:false,//是否展示错题反馈
        feedBackMsg:'',//错题反馈信息
        noteBtnColor:'#3e87f59d',//记笔记按钮颜色
        noteDivShow:false,//是否展示记笔记
        noteMsg:'',//笔记信息
        answerDivShow:false,//是否展示答题卡页面
        answerRecordData:null,//答题卡答题情况数据
        startX: 0, // 起始X坐标
        startY: 0, // 起始Y坐标
    },

    /**
     * 组件的方法列表
     */
    methods: {
        nextQuestion(event){
            //下一题
            this.triggerEvent("nextQuestion", {topicIndex:this.data.topicIndex,indexNum: this.data.topicData.index_num}, {});
        },
        doQuestions(event){
            //答题
            if(this.data.userAnswer!=null&&this.data.userAnswer!=''){
                //已经答过了，不能再答题了
            }else{
                let current=event.currentTarget.dataset.current;
                let rightmsg=event.currentTarget.dataset.rightmsg;
                let userRight=0;
                if(current==rightmsg){
                    //答对了
                    userRight=1;
                }
                 //答错了
                 let topic=this.data.topicData;
                 topic.user_answer_record=current;
                 this.setData({
                    userAnswer:current,
                    topicData:topic
                });
                //用户答题记录
                indexModel.doQuestions(8,this.data.mockId,this.data.topicData.course_id,current,this.data.topicData.id,this.data.topicData.index_num);
                //直接下一步
                setTimeout(()=>{
                    this.triggerEvent("nextQuestion", {topicIndex:this.data.topicIndex,indexNum: this.data.topicData.index_num}, {});
                }, 2000);
            }
        },
        collQuestion(event){
            //收藏/取消收藏
            let topicData=this.data.topicData;
            topicData.is_collect=(topicData.is_collect?0:1);
            this.setData({
                topicData:topicData
            });
            indexModel.collQuestion(this.data.topicData.id,this.data.topicData.is_collect,this.data.topicData.data_type,this.data.topicData.type_id,this.data.topicData.course_id);
        },
        correctShow(event){
            //错题反馈页面展示
            let correctDivShow=true;
            if(event.currentTarget.dataset.type==1){
                correctDivShow=false;
            }
            this.setData({
                correctDivShow:correctDivShow,
                feedBackMsg:''
            });
        },
        bindFeedBackInput(event){
            let feedBackBtnColor="#3e87f59d";
            if(event.detail.value!=''){
                feedBackBtnColor="#3e88f5";
            }
            this.setData({
                feedBackBtnColor:feedBackBtnColor,
                feedBackMsg:event.detail.value
            });
        },
        correctSubmit(event){
            //提交错题反馈
            let feedBackMsg=this.data.feedBackMsg;
            if(this.data.feedBackMsg!=''){
                indexModel.correctSubmit(this.data.topicData.id,feedBackMsg);
                wx.showToast({
                    title: '感谢您的反馈',
                    icon: 'none',
                    duration: 2000
                });
            }
            this.setData({
                correctDivShow:false
            });
        },
        noteShow(event){
            //记笔记页面展示
            let noteDivShow=true;
            if(event.currentTarget.dataset.type==1){
                noteDivShow=false;
            }
            this.setData({
                noteDivShow:noteDivShow,
                noteMsg:''
            });
        },
        bindNoteInput(event){
            let noteBtnColor="#3e87f59d";
            if(event.detail.value!=''){
                noteBtnColor="#3e88f5";
            }
            this.setData({
                noteBtnColor:noteBtnColor,
                noteMsg:event.detail.value
            });
        },
        noteSubmit(event){
            //提交记的笔记
            let noteMsg=this.data.noteMsg;
            if(this.data.noteMsg!=''){
                indexModel.noteSubmit(this.data.topicData.id,this.data.topicData.type_id,this.data.topicData.course_id,this.data.topicData.data_type,noteMsg);
                wx.showToast({
                    title: '记录成功',
                    icon: 'none',
                    duration: 2000
                });
            }
            this.setData({
                noteDivShow:false
            });
        },
       async answerShow(event){
            //答题卡页面展示
            let answerDivShow=true;
            if(event.currentTarget.dataset.type==1){
                answerDivShow=false;
            }else{
                //获取答题情况(章节练习和历年真题练习才有答题记录)
                let records = await indexModel.answerRecords(8,this.data.mockId);
                this.setData({
                   answerRecordData:records.topicList
                });
            }
            this.setData({
                answerDivShow:answerDivShow,
            });
        },
        selNumberTopic(event){
            let indexNum=event.currentTarget.dataset.index;
            let curIndex=indexNum%10;
            let topicIndex=(curIndex?(curIndex-1):(indexNum-1));//计算在分页的情况下是第几个
            //计算是第几页
            let clickPage=Math.ceil(indexNum/10);
            topicIndex=topicIndex-1;//外面一层下一题的时候会自动加一；这里计算出来的是目标index 这边减1；就不判断处理外面一层的加一了 
            //选择题目跳转过去
            this.triggerEvent("nextQuestion", {topicIndex:topicIndex,indexNum: indexNum,isAnswer:event.currentTarget.dataset.answer,clickPage:clickPage}, {});
            //关闭答题卡页面
            this.setData({
                answerDivShow:false
            })
        },
        examTimeing(e){
            this.triggerEvent("examTimeing", {remainingTime:e.detail.momoTime}, {});
        },
        onTouchStart(event) {
            // 记录起始坐标
            this.setData({
              startX: event.touches[0].pageX,
              startY: event.touches[0].pageY,
            });
        },
        onTouchEnd(event) {
            const endX = event.changedTouches[0].pageX;
            const endY = event.changedTouches[0].pageY;
            const diffX = endX - this.data.startX;
            const diffY = endY - this.data.startY;
            // 判断滑动方向
            if (Math.abs(diffX) > Math.abs(diffY)) {
              if (diffX > 50) {
                let topicIndex=this.data.topicIndex-2;
                let indexNum=this.data.topicData.index_num-2;
                if(topicIndex<=-1){
                    topicIndex=-1;
                }
                if(indexNum<=0){
                    indexNum=1;
                }
                this.triggerEvent("nextQuestion", {topicIndex:topicIndex,indexNum: indexNum}, {});
              } else if (diffX < -50) {
                this.triggerEvent("nextQuestion", {topicIndex:this.data.topicIndex,indexNum: this.data.topicData.index_num}, {});
              }
            }
        },
    },
})